import datetime


logFile = None  # 日志文件


def init():
    global logFile
    try:
        f = open('log/PhotoSprite.log', 'a+', encoding='utf-8')
        # with open("log/PhotoSprite.log", "a+", encoding="utf-8") as f:
        logFile = f
    except Exception:
        print('日志加载失败')
        f.close()
    else:
        time = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
        f.write('\n\n====PhotoSpriteLitle==')
        f.write('\nstartup time=')
        f.write(time)
        f.write('\nversion=')
        f.write('\n')
        f.flush()


def __write(prefix, *arg):
    f = logFile
    if not f:
        return
    time = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
    f.write('[' + time + '] ')
    f.write(prefix)
    for each in arg:
        try:
            info = str(each)
        except Exception:
            info = 'invalid string'
        f.write(info)
        f.write(' ')
    f.write('\n')
    f.flush()


def warn(*arg):
    __write('[warn]', *arg)


def error(*arg):
    __write('[error]', *arg)


def msg(*arg):
    __write('[msg]', *arg)
